package com.isoftstone.studentregistration.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web配置类，用于配置跨域请求和静态资源映射
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
    // 文件上传路径
    @Value("${file.upload.path}")
    private String fileUploadPath;
    // 文件映射路径
    @Value("${file.upload.fileMapper}")
    private String fileMapper;

    /**
     * 配置跨域请求
     * @param registry 跨域注册器
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//               .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowCredentials(true)
                .maxAge(3600);
    }

    /**
     * 配置静态资源映射
     * @param registry 资源处理器注册器
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(fileMapper + "**")
                .addResourceLocations("file:" + fileUploadPath);
    }
}
